﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weight.Base
{
    public class UtilityHelper
    {
        /// <summary>
        /// 字符串转换成int类型
        /// </summary>
        /// <param name="val">字符串</param>
        /// <param name="defautVal">默认值</param>
        /// <returns></returns>
        public static int StrToInt(string val,int defautVal)
        {
           if(string.IsNullOrEmpty(val))
            {
                return defautVal;
            }
            int result = 0;
            bool b =  int.TryParse(val,out result);
            if (!b) result = defautVal;
            return result;
        }

        /// <summary>
        /// 字符串转换成int
        /// </summary>
        /// <param name="val">字符串</param>
        /// <returns></returns>
        public static int? StrToInt(string val)
        {
            if (string.IsNullOrEmpty(val))
            {
                return null;
            }
            int result = 0;
            bool b = int.TryParse(val, out result);
            if(!b)
            {
                return null;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 字符串转换成Decimal
        /// </summary>
        /// <param name="val">字符串</param>
        /// <param name="defaultVal">默认值</param>
        /// <returns></returns>
        public static decimal StrToDecimal(string val,decimal defaultVal)
        {
            if (string.IsNullOrEmpty(val))
            {
                return defaultVal;
            }
            decimal result = 0;
            bool b = decimal.TryParse(val, out result);
            if (!b) result = defaultVal;
            return result;
        }

        /// <summary>
        /// 字符串转换成Decimal?
        /// </summary>
        /// <param name="val">字符串</param>
        /// <returns></returns>
        public static decimal? StrToDecimal(string val)
        {
            if (string.IsNullOrEmpty(val))
            {
                return null;
            }
            decimal result = 0;
            bool b = decimal.TryParse(val, out result);
            if (!b)
            {
                return null;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 字符串转换成Decimal
        /// </summary>
        /// <param name="val">字符串</param>
        /// <param name="defaultVal">默认值</param>
        /// <returns></returns>
        public static double StrToDouble(string val, double defaultVal)
        {
            if (string.IsNullOrEmpty(val))
            {
                return defaultVal;
            }
            double result = 0;
            bool b = double.TryParse(val, out result);
            if (!b) result = defaultVal;
            return result;
        }

        /// <summary>
        /// 字符串转换成Decimal?
        /// </summary>
        /// <param name="val">字符串</param>
        /// <returns></returns>
        public static double? StrToDouble(string val)
        {
            if (string.IsNullOrEmpty(val))
            {
                return null;
            }
            double result = 0;
            bool b = double.TryParse(val, out result);
            if (!b)
            {
                return null;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// object 对象转换成bool 值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static  bool ObjToBool(object obj)
        {
            if (obj == null) return false;
            bool res = false;
            bool.TryParse(Convert.ToString( obj),out res);
            if(res)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
